<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>员工工资区间统计</title>
    <!-- Load ECharts library -->
    <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.0/echarts.min.js"></script>
    <style>
        /* Optional: Some basic styling */
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        #line {
            width: 100%;
            height: 100vh;
        }
    </style>
</head>
<body>

<!-- Chart container -->
<div id="line"></div>

<script>
    // Initialize ECharts instance
    var lineDom = document.getElementById('line');
    var lineChart = echarts.init(lineDom);

    // Option for your chart
    const lineOption = {
        title: {
            text: '员工工资区间统计',
            subtext: 'Data From Mysql',
            left: 'center',
            textStyle: {
                fontSize: 24,
                fontWeight: 'bold'
            },
            subtextStyle: {
                fontSize: 16,
                color: '#666'
            }
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            formatter: '{b} : {c} 人'
        },
        xAxis: {
            type: 'category',
            data: [],
            axisLine: {
                lineStyle: {
                    color: '#333'
                }
            },
            axisLabel: {
                interval: 0,
                rotate: 45,
                textStyle: {
                    fontSize: 12,
                    color: '#333'
                }
            }
        },
        yAxis: {
            type: 'value',
            axisLine: {
                lineStyle: {
                    color: '#333'
                }
            },
            axisLabel: {
                textStyle: {
                    fontSize: 12,
                    color: '#333'
                }
            },
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }
        },
        series: [
            {
                data: [],
                type: 'bar',
                barWidth: '60%',
                itemStyle: {
                    color: '#6fb2e4'
                },
                label: {
                    show: true,
                    position: 'top',
                    textStyle: {
                        color: '#333',
                        fontSize: 12
                    }
                }
            }
        ],
        toolbox: {
            show: true,
            feature: {
                dataView: { show: true, readOnly: false },
                magicType: { show: true, type: ['line', 'bar'] },
                restore: { show: true },
                saveAsImage: { show: true }
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        }
    };

    // Fetch data from backend
    fetch("/stats/getlevelofemployee")
        .then(response => response.json())
        .then(res => {
            console.log('Response from backend:', res);
            if (res.code === 1) {
                const level = res.data.map(v => v.name);
                lineOption.xAxis.data = level;

                const count = res.data.map(v => v.count);
                lineOption.series[0].data = count;

                lineChart.setOption(lineOption);

                // Make the chart responsive
                window.addEventListener('resize', () => {
                    lineChart.resize();
                });
            } else {
                console.error('Error:', res.msg);
            }
        })
        .catch(error => console.error('Error fetching data:', error));
</script>

</body>
</html>
